package org.streets.eis.util;

import java.io.Serializable;

public class Pager implements Serializable {

	private int totalRows;  // 总行数
	private int pageSize = 30;  // 每页显示的行数
	private int currentPage;  // 当前页号
	private int totalPages;  // 总页数
	private int startRow;  // 当前页在数据库中的起始行
	private String msg;


	public Pager(int totalRows) {
		this.totalRows = totalRows;
		this.totalPages = totalRows / pageSize;
		int mod = totalRows % pageSize;
		if (mod > 0) {
			this.totalPages++;
		}
		this.currentPage = 1;
		this.startRow = 0;
	}

	public int getStartRow() {
		return startRow;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		if (currentPage > totalPages) {
			last();
		} else {
			this.currentPage = currentPage;
			this.startRow = (currentPage - 1) * pageSize;
		}
	}


	public int getPageSize() {
		return pageSize;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
		this.totalPages = totalRows / pageSize;
		int mod = totalRows % pageSize;
		if (mod > 0) {
			totalPages++;
		}
		this.currentPage = 1;
		this.startRow = 0;
	}



	public int getTotalRows() {
		return totalRows;
	}

	public void first() {
		currentPage = 1;
		startRow = 0;
	}

	public void previous() {
		if (currentPage == 1) {
			return;
		}
		currentPage--;
		startRow = (currentPage - 1) * pageSize;
	}

	public void next() {
		if (currentPage < totalPages) {
			currentPage++;
		}
		startRow = (currentPage - 1) * pageSize;
	}

	public void last() {
		currentPage = totalPages;
		startRow = (currentPage - 1) * pageSize;
	}

	public void refresh(int currentPage) {
		this.currentPage = currentPage;
		if (currentPage > totalPages) {
			last();
		}
	}

	public String getPageMsg() {
		return String.format("共%d条记录 %d页 当前第%d页", totalRows, totalPages, currentPage);
	}
}
